package cn.dancepeak.role.controller;

import cn.dancepeak.role.domain.Role;
import cn.dancepeak.role.query.RoleQuery;
import cn.dancepeak.role.service.IRoleServiceImpl;
import cn.dancepeak.exception.BusinessException;
import cn.dancepeak.utils.R;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Autowired
    private IRoleServiceImpl roleService;

    //1.增
    @PostMapping("/add")
    public R<Void> add(@RequestBody Role role){
        try {
            roleService.add(role);
            return R.ok("新增成功");
        } catch (BusinessException e) {
            log.info("新增用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("新增系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //2.根据ID改
    @PostMapping("/update")
    public R<Void> update(@RequestBody Role role){
        try {
            roleService.update(role);
            return R.ok("修改成功");
        } catch (BusinessException e) {
            log.info("修改用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("修改系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //3.根据ID删
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable("id") Long id){
        try {
            roleService.delete(id);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            log.info("删除用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //4.批量删
    @PostMapping("/deleteByList")
    public R<Void> deleteByList(@RequestBody List<Long> ids){
        try {
            roleService.deleteByList(ids);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            log.info("删除用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //5.根据ID查
    @GetMapping("/{id}")
    public R<Role> getById(@PathVariable("id") Long id){
        try {
            Role role = roleService.getById(id);
            return R.ok(role);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //6.分页查询
    @PostMapping("/page")
    public R<PageInfo<Role>> page(@RequestBody RoleQuery roleQuery){
        try {
            PageInfo<Role> page = roleService.page(roleQuery);
            return R.ok(page);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //查所有
    @GetMapping("/getAll")
    public R<List<Role>> getAll(){
        try {
            List<Role> all = roleService.getAll();
            return R.ok(all);
        } catch (BusinessException e) {
            log.info("查询所有角色异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询所有角色系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
}
